package sunwin.yoms.web.comm.controllerAspect;

import jodd.datetime.JDateTime;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.domain.comm.AdminOperateDomain;
import sunwin.yoms.domain.system.SysFunctionDomain;
import sunwin.yoms.service.security.YomsUserDetails;
import sunwin.yoms.service.system.LogService;
import sunwin.yoms.service.system.SysFunctionService;
import sunwin.yoms.web.comm.YomsController;
import sunwin.yoms.web.comm.YomsControllerDefaultData;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 田西泽 on 2015-8-26.
 * 日志
 */
@Component
public class LogAspect {
    @Autowired
    private YomsControllerDefaultData defaultData;
    @Autowired
    private SysFunctionService functionService;

    @Autowired
    private LogService logService;

    public void writeLog(HttpServletResponse response, HandlerMethod handler, boolean isAuthority) throws YomsException {
        YomsController yomsController = handler.getMethodAnnotation(YomsController.class);
        if (yomsController != null && StringUtil.isNotBlank(yomsController.functionNo())) {
            YomsUserDetails userDetails = defaultData.getCurrentYomsUserDetails();
            SysFunctionDomain functionDomain = functionService.getFuncByNo(yomsController.functionNo());

            AdminOperateDomain domain = new AdminOperateDomain();
            domain.setAdminOperateUserid(userDetails.getUserid());
            domain.setAdminOperateUsername(userDetails.getRealname());
            domain.setAdminOperateUserloginname(userDetails.getRealname());
            domain.setAdminOperateFunctionname(functionDomain.getSysFuncName());
            domain.setAdminOperateFunctionno(functionDomain.getSysFuncNo());
            //操作状态。1.成功；2.失败；
            domain.setAdminOperateState(isAuthority ? 1 : 2);
            if(!isAuthority){
                domain.setAdminOperateFailCause("没有权限");
            }
            domain.setAdminOperateDatetime(new JDateTime().convertToDate());

            String subregionName = logService.addLog(domain);

            response.addHeader("yomsLogId", domain.getAdminOperateId() + "");
            response.addHeader("yomsLogSubregion", subregionName);
        }
    }

    /**
     * 更新日志
     *
     * @param response
     * @param request
     * @param ex
     * @throws YomsException
     */
    public void updateLog(HttpServletResponse response, HttpServletRequest request, Exception ex) throws YomsException {
        if (response.getHeader("yomsLogId") != null && response.getHeader("yomsLogSubregion") != null) {
            if (ex != null) {
                int id = Integer.parseInt(response.getHeader("yomsLogId") + "");
                String subregionName = String.valueOf(response.getHeader("yomsLogSubregion"));

                AdminOperateDomain domain = new AdminOperateDomain();
                domain.setAdminOperateId(id);
                domain.setAdminOperateFailCause(ex.getClass().toString());

                logService.updateLog(domain, subregionName);
            }
        }
    }
}